package com.mrjy.search.bo;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Tuple;

import com.mrjy.common.entity.CoursePackageInfo;
import com.mrjy.common.entity.HotSearchKeywords;
import com.mrjy.common.entity.QuestionComment;
import com.mrjy.common.entity.QuestionInfo;
import com.mrjy.common.entity.SolrEntity;
import com.mrjy.common.entity.TeacherInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.entity.UserMemberInfo;
import com.mrjy.common.entity.UserVideoRel;
import com.mrjy.common.entity.VideoInfo;
import com.mrjy.common.redis.RedisPool;
import com.mrjy.common.solr.SolrServerClient;
import com.mrjy.common.util.DateUtil;
import com.mrjy.common.util.ExcelUtil;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.common.util.RedisUtil;
import com.mrjy.coursepackage.dao.ICoursePackageInfoDao;
import com.mrjy.coursepackage.dto.CoursePackageInfoDTO;
import com.mrjy.maintain.dao.ITeacherInfoDao;
import com.mrjy.maintain.dao.IUserInfoDao;
import com.mrjy.maintain.dao.IUserMemberInfoDao;
import com.mrjy.maintain.dto.TeacherInfoDTO;
import com.mrjy.queries.dao.IQuestionCommentDao;
import com.mrjy.queries.dao.IQuestionInfoDao;
import com.mrjy.search.dao.IHotSearchKeywordsDao;
import com.mrjy.video.dao.IUserVideoRelDao;
import com.mrjy.video.dao.IVideoInfoDao;

@Service
public class SearchBo implements ISearchBo {
	
	@Resource
	private SolrServerClient solrServerClient;
	@Resource
	private IVideoInfoDao videoInfoDao;
	@Resource
	private ICoursePackageInfoDao coursePackageInfoDao;
	@Resource
	private IUserInfoDao userInfoDao;
	@Resource
	private IQuestionInfoDao questionInfoDao;
	@Resource
	private IQuestionCommentDao questionCommentDao;
	@Resource
	private RedisPool redisPool;
	@Resource
	private IUserMemberInfoDao userMemberInfoDao;
	@Resource
	private IHotSearchKeywordsDao hotSearchKeywordsDao;
	@Resource
	private ITeacherInfoDao teacherInfoDao;
	@Resource
	private IUserVideoRelDao userVideoRelDao;
	
	@Value("${solr.url}") 
	private String solrHost;
	
	@Override
	public Object importSolr() {
		try {
			HttpSolrServer solrServer = new HttpSolrServer(solrHost);
			solrServer.deleteByQuery("*:*");
			solrServer.commit();
			/*
			 * 导入微课
			 */
			VideoInfo vinfo = new VideoInfo();
			List<VideoInfo> vList = videoInfoDao.queryListForImportSolr(vinfo);
			for (VideoInfo videoInfo : vList) {
				//待发布的视频先不放在solr里去，待触发器触发后在添加上去
				if(videoInfo.getReleaseFlag() != 1){
					SolrInputDocument document = new SolrInputDocument();
					document.addField("id", "video_"+videoInfo.getId());
					document.addField("videoId", videoInfo.getId());
					document.addField("videoName", videoInfo.getVideoName());
					document.addField("videoTypes", videoInfo.getTypeName());
					document.addField("videoTeacher", videoInfo.getUserName());
					document.addField("coverUrl", videoInfo.getCoverUrl());
					solrServer.add(document);
				}
			}
			/*
			 * 导入提问
			 */
			QuestionInfo qinfo = new QuestionInfo();
			List<QuestionInfo> qList = questionInfoDao.queryListForImportSolr(qinfo);
			for (QuestionInfo questionInfo : qList) {
				SolrInputDocument document = new SolrInputDocument();
				document.addField("id", "question_"+questionInfo.getId());
				document.addField("questionId", questionInfo.getId());
				document.addField("questionContent", questionInfo.getQuestionContent());
				document.addField("questionDesc", questionInfo.getQuestionDesc());
				document.addField("questionTypes", questionInfo.getTypeName());
				solrServer.add(document);
			}
			/*
			 * 导入课程包
			 */
			CoursePackageInfo cinfo = new CoursePackageInfo();
			List<CoursePackageInfoDTO> cpList = coursePackageInfoDao.queryListForImportSolr(cinfo);
			for (CoursePackageInfoDTO coursePackageInfo : cpList) {
				SolrInputDocument document = new SolrInputDocument();
				document.addField("id", "coursePackage_"+coursePackageInfo.getId());
				document.addField("coursePackageId", coursePackageInfo.getId());
				document.addField("coursePackageName", coursePackageInfo.getCoursePackageName());
				document.addField("coursePackageTypes", coursePackageInfo.getTypeName());
				document.addField("coursePackageTeacher", coursePackageInfo.getUserName());
				document.addField("coverUrl", coursePackageInfo.getCoverUrl());
				solrServer.add(document);
			}
			/*
			 * 导入回答
			 */
			QuestionComment cominfo = new QuestionComment();
			List<QuestionComment> comList = questionCommentDao.queryListForImportSolr(cominfo);
			for (QuestionComment questionComment : comList) {
				SolrInputDocument document = new SolrInputDocument();
				document.addField("id", "questionComment_"+questionComment.getId());
				document.addField("commentId", questionComment.getId());
				document.addField("commentContent", questionComment.getCommentContent());
				document.addField("recordName", questionComment.getRecordName());
				document.addField("recordDuration", questionComment.getDuration());
				document.addField("questionContent", questionComment.getQuestionContent());
				document.addField("questionId", questionComment.getQuestionId());
				document.addField("headImgUrl", questionComment.getHeadImgUrl());
				document.addField("picName", questionComment.getPicName());
				document.addField("userId", questionComment.getUserId());
				document.addField("replyType", questionComment.getReplyType());
				document.addField("questionTypes", questionComment.getQuestionTypes());
				solrServer.add(document);
			}
			/*
			 * 导入讲师
			 */
			TeacherInfo tinfo = new TeacherInfo();
			List<TeacherInfoDTO> tList = teacherInfoDao.queryListForImportSolr(tinfo);
			for (TeacherInfoDTO teacherInfo : tList) {
				SolrInputDocument document = new SolrInputDocument();
				document.addField("id", "teacher_"+teacherInfo.getId());
				document.addField("userId", teacherInfo.getUserId());
				document.addField("teacherId", teacherInfo.getId());
				document.addField("teacherName", teacherInfo.getUserName());
				document.addField("teacherDesc", teacherInfo.getUserDesc());
				document.addField("teacherTypes", teacherInfo.getTypeName());
				document.addField("headImgUrl", teacherInfo.getHeadImgUrl());
				document.addField("picName", teacherInfo.getPicName());
				document.addField("recordAuthority", teacherInfo.getRecordAuthority());
				solrServer.add(document);
			}
			solrServer.commit();
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Object doSearch(String q,Integer type,Integer page, Integer rows, HttpServletRequest request,
			HttpServletResponse response,HttpSession session) {
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		Map<String, Object> map = new HashMap<String, Object>();
		List<SolrEntity> courseList = new ArrayList<>();
        List<SolrEntity> teacherList = new ArrayList<>();
        List<SolrEntity> QAList = new ArrayList<>();
		Map<String, Object> resultMap = null;
		//solr检索
		try {
			if (type == null) {
				page = 1;
				rows = 100;
			}
			resultMap = solrServerClient.doSearch(q, type, page, rows, true);
			courseList = (List<SolrEntity>) resultMap.get("courseList");
			teacherList = (List<SolrEntity>) resultMap.get("teacherList");
			QAList = (List<SolrEntity>) resultMap.get("QAList");
			
			//封装课程数据
			for (SolrEntity solrEntity : courseList) {
				//视频
				if (solrEntity.getVideoId()!=null) {
					VideoInfo vInfo = (VideoInfo) videoInfoDao.queryById(solrEntity.getVideoId());
                    if (vInfo != null) {
                        UserVideoRel userVideoRel = new UserVideoRel();
                        userVideoRel.setUserId(loginUser.getId());
                        userVideoRel.setVideoId(vInfo.getId());
                        UserVideoRel userVideoRel1 = userVideoRelDao.queryInfoByUserIdAndVideoId(userVideoRel);

                        if (userVideoRel1 != null) {
                            solrEntity.setWeclassIsFinish(userVideoRel1.getWeclassIsFinish());
                        } else {
                            solrEntity.setWeclassIsFinish(0);
                        }

						solrEntity.setClickCount(vInfo.getClickCount());//xxx人看
						solrEntity.setPolyInfo(RedisUtil.cache(solrEntity.getVideoId()+""));
                        solrEntity.setFreeFlag(vInfo.getFreeFlag());
                        solrEntity.setOriginalPrice(vInfo.getOriginalPrice());
                        solrEntity.setIsSpecial(vInfo.getIsSpecial());
                        solrEntity.setPicName(vInfo.getUserPic());
                        solrEntity.setUserId(vInfo.getUserId());
                        solrEntity.setDuration(vInfo.getDuration());

                        if (vInfo.getFreeFlag() == 0) {
                            Boolean videoVip = RedisUtil.isVideoVip(loginUser.getId(), solrEntity.getVideoId());
                            if (videoVip) {
                                solrEntity.setIsVipStatus(1);
                            } else {
                                solrEntity.setIsVipStatus(0);
                            }
                        } else {
                            solrEntity.setIsVipStatus(0);
                        }
                    }
//					//查观看进度
//					UserVideoRel rel = new UserVideoRel();
//					rel.setUserId(loginUser.getId());
//					rel.setVideoId(solrEntity.getVideoId());
//					UserVideoRel videoRel = userVideoRelDao.queryInfoByUserIdAndVideoId(rel);
//					if (videoRel!=null && videoRel.getIsFinish().equals("1")) {
//						solrEntity.setViewProgress(1d);
//					}else {
//						solrEntity.setViewProgress(videoRel==null?0d:videoRel.getViewProgress());
//					}
				}else if(solrEntity.getCoursePackageId()!=null){
				//课程包
					CoursePackageInfoDTO vo = new CoursePackageInfoDTO();
					vo.setId(solrEntity.getCoursePackageId());
					vo.setUserId(loginUser==null?null:loginUser.getId());
					CoursePackageInfoDTO cInfo = coursePackageInfoDao.selectById(vo);
					if (cInfo!=null) {
						solrEntity.setCoursePackageScore(cInfo.getAverageStart().doubleValue());
						solrEntity.setClickCount(cInfo.getClickCount());
						solrEntity.setIsSpecial(cInfo.getIsSpecial());
						solrEntity.setLearnerNum(cInfo.getLearnerNum());
						//观看进度
						solrEntity.setViewProgress(cInfo.getViewProgress());
					}
				}
			}
			//封装讲师数据
			for (SolrEntity solrEntity : teacherList) {
				if (solrEntity.getUserId()!=null) {
					UserInfo tInfo = userInfoDao.getTeacherInfoById(solrEntity.getUserId());
					if (tInfo!=null) {
						solrEntity.setTeacherGoodNum(tInfo.getTotalGoodNum());//总获赞数
						solrEntity.setTeacherViewNum(tInfo.getTotalClickCount());//总观看量
					}
				}
			}
			//封装问答数据
			for (SolrEntity solrEntity : QAList) {
				//回答
				if (solrEntity.getCommentId() != null) { 
					QuestionComment comment = new QuestionComment();
					comment.setId(solrEntity.getCommentId());
					comment.setGoodUserId(loginUser==null?null:loginUser.getId());//登录用户
					comment = questionCommentDao.queryById(comment);
					UserMemberInfo userMemberInfo = userMemberInfoDao.selectIsVipByUserId(loginUser==null?null:loginUser.getId());
					if (comment!=null ) {
						if (comment.getIsNeedPrivilege()!=null 
								&& comment.getIsNeedPrivilege().equals(1)) { //付费回答需要返回权限
							if (comment.getTimeOut() == 0) {
								//免费三小时
								solrEntity.setRecordStatus(0);
							} else if ((comment.getGoodUserId() != null) && loginUser!=null && (loginUser.getId().equals(comment.getUserId()) || loginUser.getId().equals(comment.getCommentedId()))) {
								solrEntity.setRecordStatus(1);//导师判断,回答人和当前用户一直就是受益人
							} else if (null != userMemberInfo) {//会员
								//用户可以直接收听语音，返回权限通过
								solrEntity.setRecordStatus(4);
							} else if (comment.getUbpId() != null) {
								//用户可以直接收听语音，返回权限通过
								solrEntity.setRecordStatus(2);
							} else {
								//订单没有支付
								solrEntity.setRecordStatus(3);
							}
						}
						solrEntity.setGoodFlag(comment.getGoodFlag());
						solrEntity.setCommentNum(comment.getCommentNum());
						solrEntity.setCommentGoodNum(comment.getGoodNum());
					}
					
				}else if(solrEntity.getCommentId() == null 
						&& solrEntity.getQuestionId() != null){
				//提问
					QuestionInfo info = new QuestionInfo();
					info.setId(solrEntity.getQuestionId());
					QuestionInfo qInfo = questionInfoDao.queryById(info);
					if (qInfo!=null) {
						solrEntity.setQuestionCommentNum(qInfo.getCommentNum());//提问回答数
						solrEntity.setQuestionViewNum(qInfo.getQuestionViewNum());//提问浏览数
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("err_code", 500);
			map.put("err_msg", "查询失败");
			return map;
		}
		
		
		//搜索历史存入cookie  未登录则没有搜索历史
		if (loginUser!=null) {
			String historyValue = redisPool.get("USER_SEARCH_HISTORY_"+loginUser.getId());
			if (StringUtils.isNotBlank(historyValue)) {//有搜索历史
				LinkedList<String> list = new LinkedList<>(Arrays.asList(historyValue.split("@@@")));
				//若关键词存在搜索历史中,则移除,添加到首位
				if (list.contains(q)) {
					list.remove(q);
				}
				list.addFirst(q);
				if (list.size()>3) {
					list.removeLast();
				}
				redisPool.set("USER_SEARCH_HISTORY_"+loginUser.getId(), StringUtils.join(list.toArray(), "@@@"));
			}else {//第一次搜索
				redisPool.set("USER_SEARCH_HISTORY_"+loginUser.getId(), q);
			}
		}
		
		//添加热搜
		Double score = redisPool.zincrby("HOT_SEARCH_KEYWORDS", 1, q);
		HotSearchKeywords keywords = new HotSearchKeywords();
		keywords.setKeywords(q);
		if (score.intValue()==1) { //返回的score为1,则说明是新增的热搜词,插入数据库 
			keywords.setCreateTime(new Timestamp(System.currentTimeMillis()));
			keywords.setDisableFlag(0);
			keywords.setScore(score);
			hotSearchKeywordsDao.insert(keywords);
		}else { // 否则说明该词已存在,更新数据库score
			keywords.setScore(score);
			hotSearchKeywordsDao.updateByKeywords(keywords);
		}
		
		map.put("err_code", 200);
		map.put("err_msg", "查询成功");
		map.put("total", resultMap.get("numFound"));
		map.put("courseList", resultMap.get("courseList"));
        map.put("teacherList", resultMap.get("teacherList"));
        map.put("QAList", resultMap.get("QAList"));
		return map;
	}
	
	@Override
	public Object listSearchHistory(HttpSession session) {
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		String searchHistory = null;
		if (loginUser!=null) {
			searchHistory = redisPool.get("USER_SEARCH_HISTORY_"+loginUser.getId());
		}
		return searchHistory;
	}

	@Override
	public Object txDeleteSearchHistory(HttpSession session, Integer index) {
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String searchHistory = redisPool.get("USER_SEARCH_HISTORY_"+loginUser.getId());
			String[] delBefore = searchHistory.split("@@@");//删除前
			String[] delAfter = (String[]) ArrayUtils.remove(delBefore, index); //删除后
			redisPool.set("USER_SEARCH_HISTORY_"+loginUser.getId(),StringUtils.join(delAfter, "@@@"));
			map.put("err_code", 200);
			map.put("err_msg", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("err_code", 500);
			map.put("err_msg", "删除失败");
		}
		return map;
	}

	@Override
	public Object listHotSearch(HttpSession session) {
		Set<Tuple> set = (Set<Tuple>) redisPool.zrevrange("HOT_SEARCH_KEYWORDS", 0, 5); //score递减排列,取出前6个
		List<HotSearchKeywords> list = new ArrayList<>();
		for (Tuple tuple : set) {
			HotSearchKeywords keywords = new HotSearchKeywords();
			keywords.setKeywords(tuple.getElement());
			keywords.setScore(tuple.getScore());
			list.add(keywords);
		}
		return list;
	}

	@Override
	public Object queryPage(HttpSession session, HotSearchKeywords keywords) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
//		if(null == loginUser ){
//			map.put("err_code", -1);
//			map.put("err_msg", "用户未登录");
//			return map;
//		}
		int count = hotSearchKeywordsDao.countPageHotSearch(keywords);
		long totalScore = hotSearchKeywordsDao.queryTotalScore(keywords);
		List<HotSearchKeywords> list = null;
		if (count > 0) {
			list = hotSearchKeywordsDao.queryPageHotSearch(keywords);
			map.put("err_code", 200);
			map.put("err_msg", "查询成功");
		}else {
			map.put("err_code", 500);
			map.put("err_msg", "查询成功,无数据");
		}
		map.put("rows", list);
		map.put("total", count);
		map.put("totalScore", totalScore);
		return map;
	}

	@Override
	public Object txInsertHotSearch(HttpSession session,
			HotSearchKeywords keywords) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
//		if(null == loginUser ){
//			map.put("err_code", -1);
//			map.put("err_msg", "用户未登录");
//			return map;
//		}
		if (StringUtils.isBlank(keywords.getKeywords())) {
			map.put("err_code", 400);
			map.put("err_msg", "关键词不能为空");
			return map;
		}
		if (hotSearchKeywordsDao.queryByKeywords(keywords.getKeywords())!=null) {
			map.put("err_code", 401);
			map.put("err_msg", "该关键词已存在");
			return map;
		}
		if (keywords.getSort()==null) {
			map.put("err_code", 402);
			map.put("err_msg", "排序不能为空");
			return map;
		}
		keywords.setCreateTime(new Timestamp(System.currentTimeMillis()));
		keywords.setScore(0d);
		keywords.setDisableFlag(0);
		try {
			hotSearchKeywordsDao.insert(keywords);
			map.put("err_code", 200);
			map.put("err_msg", "添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("err_code", 500);
			map.put("err_msg", "添加失败");
		}
		return map;
	}
	
	@Override
	public Object txUpdateHotSearch(HttpSession session,
			HotSearchKeywords keywords) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
//		if(null == loginUser ){
//			map.put("err_code", -1);
//			map.put("err_msg", "用户未登录");
//			return map;
//		}
		HotSearchKeywords original = hotSearchKeywordsDao.queryById(keywords.getId());
		if (StringUtils.isBlank(keywords.getKeywords())) {
			map.put("err_code", 400);
			map.put("err_msg", "关键词不能为空");
			return map;
		}
		if (!original.getKeywords().equals(keywords.getKeywords()) 
				&& hotSearchKeywordsDao.queryByKeywords(keywords.getKeywords())!=null) {
			map.put("err_code", 401);
			map.put("err_msg", "该关键词已存在");
			return map;
		}
		try {
			//若关键词名称改变,更新redis
			if (!original.getKeywords().equals(keywords.getKeywords())) {
				//先移除
				redisPool.zrem("HOT_SEARCH_KEYWORDS", original.getKeywords());
				//再新增
				redisPool.zadd("HOT_SEARCH_KEYWORDS", original.getScore(), keywords.getKeywords());
			}
			hotSearchKeywordsDao.updateById(keywords);
			map.put("err_code", 200);
			map.put("err_msg", "更新成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("err_code", 500);
			map.put("err_msg", "更新失败");
		}
		return map;
	}

	@Override
	public Object txUpdateDisableFlag(HttpSession session,
			HotSearchKeywords keywords) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
//		if(null == loginUser){
//			map.put("err_code", -1);
//			map.put("err_msg", "用户未登录");
//			return map;
//		}
		if (keywords.getIds()==null||keywords.getIds().length==0) {
			map.put("err_code", 400);
			map.put("err_msg", "请勾选要操作的记录");
			return map;
		}
		try {
			int row = hotSearchKeywordsDao.updateDisableFlag(keywords);
			if (row > 0) {
				for (Integer id : keywords.getIds()) {
					HotSearchKeywords hotSearch = hotSearchKeywordsDao.queryById(id);
					if (keywords.getDisableFlag() == 1) {
						//数据库禁用,则移除redis中的数据
						redisPool.zrem("HOT_SEARCH_KEYWORDS", hotSearch.getKeywords());
					}else {
						//数据库启用,则添加redis数据
						redisPool.zincrby("HOT_SEARCH_KEYWORDS", hotSearch.getScore(), hotSearch.getKeywords());
					}
				}
				map.put("err_code", 200);
				map.put("err_msg", "成功操作"+row+"条记录");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("err_code", 500);
			map.put("err_msg", "操作失败");
		}
		return map;
	}

	@Override
	public Object txDeleteHotSearch(HttpSession session,
			HotSearchKeywords keywords) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
//		if(null == loginUser){
//			map.put("err_code", -1);
//			map.put("err_msg", "用户未登录");
//			return map;
//		}
		if (keywords.getIds()==null||keywords.getIds().length==0) {
			map.put("err_code", 400);
			map.put("err_msg", "请勾选要操作的记录");
			return map;
		}
		try {
			for (Integer id : keywords.getIds()) {
				//移除redis中的记录
				HotSearchKeywords hotSearch = hotSearchKeywordsDao.queryById(id);
				redisPool.zrem("HOT_SEARCH_KEYWORDS", hotSearch.getKeywords());
			}
			int row = hotSearchKeywordsDao.deleteByIds(keywords);
			map.put("err_code", 200);
			map.put("err_msg", "成功删除"+row+"条记录");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("err_code", 500);
			map.put("err_msg", "删除失败");
		}
		return map;
	}

	@Override
	public Object exportExl(HttpSession session, HttpServletResponse response, HotSearchKeywords keywords) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
//		if(null == loginUser){
//			map.put("err_code", -1);
//			map.put("err_msg", "用户未登录");
//			return map;
//		}
		List<HotSearchKeywords> dataset = hotSearchKeywordsDao.queryPageHotSearch(keywords);
		if (dataset == null || dataset.size()==0) {
			map.put("err_code", 404);
			map.put("err_msg", "无可导出数据");
			return map;
		}
		try {
			ExcelUtil.writeExcel(response, "热搜关键词"+DateUtil.dateToString(new Date(), DateUtil.FULL_DATE_STR)+".xls", dataset, HotSearchKeywords.class);
			map.put("err_code", 200);
			map.put("err_msg", "导出成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("err_code", 500);
			map.put("err_msg", "导出失败");
		}
		return map;
	}

}
